home *** CD-ROM | disk | FTP | other *** search
/ MacWorld 1995 November / Macworld Nov ’95.toast / Developers / Sample LDEFs 2.0 / (Sys 6⁄7) Icon LDEF / Icon LDEF.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-06-27  |  4.1 KB  |  127 lines  |  [TEXT/KAHL]

  1. // File "Icon LDEF.c" - System 6 and 7 Icon LDEF Routine  
  2. // This code is placed in the public domain for free use and distribution - MJS
  3. //
  4. //    6/27/95       Changed the Hilite Mode to use accessor functions
  5. //                  Properly save and restore Handle states - MJS
  6. //
  7. //   10/16/93        First released the LDEFs to the net - MJS
  8. //
  9. //    5/23/91       Original snippet which this is shamelessly based on,
  10. //                  by Steven Falkenburg, Apple DTS
  11.  
  12. // * **************************************************************************** * //
  13.  
  14. #include <GestaltEqu.h>
  15. #include <Icons.h>
  16.  
  17. // * **************************************************************************** * //
  18. // * **************************************************************************** * //
  19. // Universal Headers users can replace these with LM accessor functions
  20.  
  21. pascal unsigned char GetHiliteMode(void) = { 0x1EB8, 0x0938 }; /* MOVE.B 0x0938,(A7) */
  22. pascal void SetHiliteMode(unsigned char) = { 0x11DF, 0x0938 }; /* MOVE.B (A7)+,0x0938 */
  23.  
  24. // * **************************************************************************** * //
  25. // * **************************************************************************** * //
  26.  
  27. pascal void    main(short message, Boolean hilited, Rect *cellRect, Cell theCell,
  28.         short dataOffset, short dataLen, ListHandle theList) {
  29.     short iconID, hasSys7;
  30.     long response;
  31.     Point drawPt;
  32.     Rect iconRect, textRect;
  33.     RgnHandle iconRgn;
  34.     BitMap iconMap;
  35.     Ptr cellData;
  36.     Handle iconHdl;
  37.     ListPtr theListPtr;
  38.     SignedByte hStateList, hStateCells;
  39.     FontInfo fontInfo;
  40.  
  41.     if (Gestalt(gestaltSystemVersion, &response)) return;
  42.     hasSys7 = (response >= 0x0700);
  43.     
  44.     // Lock down the handles
  45.     hStateList = HGetState((Handle) theList);
  46.     HLock((Handle) theList);
  47.     theListPtr = *theList;
  48.     hStateCells = HGetState((Handle) theListPtr->cells);
  49.     HLock((Handle) theListPtr->cells);
  50.     cellData = *(theListPtr->cells);
  51.     
  52.     GetFontInfo(&fontInfo);
  53.     SetRect(&iconRect, cellRect->left + (theListPtr->cellSize.h >> 1) - 16,
  54.             cellRect->top + ((theListPtr->cellSize.v - fontInfo.ascent) >> 1) - 16,
  55.             cellRect->left + (theListPtr->cellSize.h >> 1) + 16,
  56.             cellRect->top + ((theListPtr->cellSize.v - fontInfo.ascent) >> 1) + 16);
  57.     SetPt(&drawPt, (iconRect.left + iconRect.right) >> 1, 
  58.             iconRect.bottom + fontInfo.ascent + 2);
  59.  
  60.     switch (message) {
  61.         case lInitMsg:
  62.               break;
  63.  
  64.         case lDrawMsg:
  65.             EraseRect(cellRect);
  66.         case lHiliteMsg:
  67.             
  68.               if (dataLen > 0) {
  69.                   if (dataLen >= 2) {
  70.                       BlockMove(cellData + dataOffset, &iconID, sizeof(iconID));
  71.                       if (iconID == 0) EraseRect(&iconRect);
  72.                       else if ((hasSys7 == 0) || PlotIconID(&iconRect, 0,
  73.                             (hilited) ? ttSelected : 0, iconID)) {
  74.                         if (iconHdl = GetResource('ICN#', iconID)) {
  75.                             HLock(iconHdl);
  76.  
  77.                             iconMap.baseAddr = *iconHdl;
  78.                             iconMap.rowBytes = 4;
  79.                             SetRect(&iconMap.bounds,0,0,32,32);
  80.                             CopyBits(&iconMap, &theListPtr->port->portBits,
  81.                                     &iconMap.bounds, &iconRect, srcCopy, 0);
  82.                             
  83.                             if (hilited) {        
  84.                                 iconMap.baseAddr += 128;
  85.                                 CopyBits(&iconMap, &theListPtr->port->portBits,
  86.                                         &iconMap.bounds, &iconRect, srcXor, 0);
  87.                                 }
  88.                                 
  89.                             HUnlock(iconHdl);
  90.                             ReleaseResource(iconHdl);
  91.                             }
  92.                           else EraseRect(&iconRect);
  93.                         }
  94.                       dataLen -= sizeof(iconID);
  95.                       dataOffset += sizeof(iconID);
  96.                       }
  97.                 
  98.                 // Condense if the text doesnt fit
  99.                 if (TextWidth(cellData, dataOffset, dataLen) >
  100.                         (cellRect->right - cellRect->left)) TextFace(condense);
  101.         
  102.                 SetRect(&textRect, drawPt.h - (TextWidth(cellData, dataOffset, dataLen) >> 1) - 2,
  103.                         drawPt.v - fontInfo.ascent - 1,
  104.                         drawPt.h + (TextWidth(cellData, dataOffset, dataLen) >> 1) + 2,
  105.                         drawPt.v + 2);
  106.  
  107.                 EraseRect(&textRect);
  108.                   MoveTo(drawPt.h - (TextWidth(cellData, dataOffset, dataLen) >> 1), drawPt.v);
  109.                 DrawText(cellData, dataOffset, dataLen);
  110.  
  111.                 if (hilited) {            
  112.                       SetHiliteMode(GetHiliteMode() ^ (1L << hiliteBit));    
  113.                       InvertRect(&textRect);
  114.                       }
  115.                   }
  116.         
  117.               break;
  118.  
  119.         case lCloseMsg:
  120.               break;
  121.             }
  122.     
  123.     // Restore the Handles
  124.     HSetState((Handle) theListPtr->cells,hStateCells);
  125.     HSetState((Handle) theList, hStateList);
  126.     }
  127.